package org.example;

import org.example.scoredata.Score;
import org.example.topicdata.Topic;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;

public class Server {
    public static ArrayList<Topic> topicarrayList = new ArrayList<>();
    public static ArrayList<Score> SocrearrayList = new ArrayList<>();
    static Connection con;
    ResultSet rs;
    public static void main(String[] args) throws SQLException {
        //连接数据库

        try{Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        }
        catch(Exception e){
            System.out.println("未成功链接数据库驱动");
        }
        try{ //创建名为UserList的数据库：
            String uri = "jdbc:derby:UserList;create=true";
            con = DriverManager.getConnection(uri); //链接数据库
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }                                                 //数据库操作

        ServerSocket server=null;
        Runnable target;
        Thread threadForClient = null;
        Socket socketOnServer = null;
        while(true) {
            try{  server=new ServerSocket(4331);
            }
            catch(IOException e1) {
                System.out.println("正在监听");
            }
            try{ System.out.println(" 等待客户呼叫");
                socketOnServer = server.accept();
                System.out.println("客户的地址:"+
                        socketOnServer.getInetAddress());
            }
            catch(IOException e) {
                System.out.println("正在等待客户");
            }
            if(socketOnServer!=null) {
                target = new ServerThread(socketOnServer);
                threadForClient =new Thread(target);
                threadForClient.start();
            }
        }
    }

}